package com.dmcb.flutter_location;

import android.util.Log;

import androidx.annotation.NonNull;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tencent.map.geolocation.TencentLocation;
import com.tencent.map.geolocation.TencentLocationListener;
import com.tencent.map.geolocation.TencentLocationManager;
import com.tencent.map.geolocation.TencentLocationRequest;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;

/**
 * FlutterLocationPlugin
 */
public class FlutterLocationPlugin implements FlutterPlugin, MethodCallHandler {

    private MethodChannel channel;

    private TencentLocationManager mLocationManager;

    private TencentLocationRequest mRequest;

    private Result mCurrentResult;

    @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
        channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "flutter_location");
        channel.setMethodCallHandler(this);
        mLocationManager = TencentLocationManager.getInstance(flutterPluginBinding.getApplicationContext());
    }

    @Override
    public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
        if (call.method.equals("getPlatformVersion")) {
            result.success("Android " + android.os.Build.VERSION.RELEASE);
        } else if (call.method.equals("initLocation")) {
            initLocation(result);
        } else if (call.method.equals("getLocation")) {
            getLocation(result);
        } else {
            result.notImplemented();
        }
    }

    /**
     * 初始化定位
     *
     * @param result
     */
    private void initLocation(@NonNull Result result) {
        mRequest = TencentLocationRequest.create();
        mRequest.setInterval(10000);
        //设置请求级别
        mRequest.setRequestLevel(TencentLocationRequest.REQUEST_LEVEL_ADMIN_AREA);
        //是否允许使用GPS
        mRequest.setAllowGPS(true);
        //是否需要获取传感器方向
        mRequest.setAllowDirection(true);
        //是否需要开启室内定位
        mRequest.setIndoorLocationMode(true);
        result.success("腾讯定位SDK 初始化成功");
        Log.d("substring", "腾讯定位SDK 初始化成功");
    }

    TencentLocationListener mListener = new TencentLocationListener() {
        @Override
        public void onLocationChanged(TencentLocation tencentLocation, int i, String s) {
            Log.d("substring", "onLocationChanged " + s);
            if (mCurrentResult != null && tencentLocation != null && tencentLocation.getCity() != null) {
                JSONObject mJSONObject = new JSONObject();
                mJSONObject.put("city", tencentLocation.getCity());
                mJSONObject.put("province", tencentLocation.getProvince());
                mJSONObject.put("latitude", tencentLocation.getLatitude());
                mJSONObject.put("longitude", tencentLocation.getLongitude());
                mCurrentResult.success(JSON.toJSONString(mJSONObject));
                mLocationManager.removeUpdates(mListener);
            }
        }

        @Override
        public void onStatusUpdate(String s, int i, String s1) {
            Log.d("substring", "onStatusUpdate " + s);
            Log.d("substring", "onStatusUpdate " + i);
            Log.d("substring", "onStatusUpdate " + s1);
        }
    };

    /**
     * 开始获取定位
     *
     * @param result
     */
    private void getLocation(@NonNull Result result) {
        if (mLocationManager != null && mRequest != null) {
            mLocationManager.requestLocationUpdates(mRequest, mListener);
            mCurrentResult = result;
        } else {
            result.error("-1", "腾讯定位SDK 定位失败", "-1");
        }
    }

    @Override
    public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
        channel.setMethodCallHandler(null);
        if (mLocationManager != null) {
            mLocationManager.removeUpdates(mListener);
        }
    }
}
